package org.kmfdm.horrorscope.tapestry.pages.user;





import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.kmfdm.horrorscope.core.spring.ApplicationContextProviderSingleton;
import org.kmfdm.horrorscope.metamodel.Customer;
import org.kmfdm.horrorscope.service.RegisterCustomer;
import org.kmfdm.horrorscope.tapestry.pages.Info;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.WebApplicationContext;

public class Register {
	
	private Customer customer;
	@Inject
	private RegisterCustomer customerRegistration;
	@InjectPage
	private Info info;
	

	
	@Component(id="customerRegistration")
	private Form customerRegistrationForm;
	
	private ApplicationContext applicationContext(){
		return new ApplicationContextProviderSingleton().getContext();
	}
	private Customer createCustomer(){
		return (Customer) applicationContext().getBean(Customer.class.getName());
	}
	public void setCustomer(Customer customer) {
		this.customer = customer;
	}
	public Customer getCustomer() {
		if(customer==null){
			customer=createCustomer();
		}
		return customer;
	}
	@OnEvent(component="customerRegistration")
	public Object onSuccess(){
		
		customerRegistration.registerCustomer(customer);
		
		info.setMessage("Registration successful!");
		return info;
		
	}
	
	
}
